package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    //分类分页查询
    @Override
    public PageResult list(CategoryPageQueryDTO dto) {
        //设置分页参数
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        //调用mapper层进行查询
        Page page= categoryMapper.list(dto.getName(),dto.getType());

        //封装数据并返回
        return new PageResult(page.getTotal(),page.getResult());
    }
    //修改分类
    @Override
    public void updateCategory(CategoryDTO dto) {
        //补全数据
        Category category = new Category();
        //复制数据
        BeanUtils.copyProperties(dto,category);
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        //调用mapper层修改数据
        log.info("检查分类信息{}",category);
        categoryMapper.updateCategory(category);
    }
    //启用禁用分类
    @Override
    public void statusUpdate(Integer status, Long id) {
        Category category = new Category();
        category.setStatus(status);
        /*category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());*/
        category.setId(id);
        categoryMapper.updateCategory(category);
    }
    //新增分类
    @Override
    public void insertCategory(CategoryDTO dto) {
        //补全数据
        Category category = new Category();
        BeanUtils.copyProperties(dto,category);
        category.setStatus(0);
     /*   category.builder()
                .status(0)
               *//* .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .createUser(BaseContext.getCurrentId())
                .updateUser(BaseContext.getCurrentId())*//*
                .build();*/
        //调用mapper层对数据进行新增处理
        categoryMapper.insertCategory(category);
    }
    //根据id删除分类
    @Override
    public void deleteCategory(Long id) {
    categoryMapper.deleteCategory(id);
    }
    //根据类型查询分类
    @Override
    public List<Category> getList(Integer type) {
        List<Category> result = categoryMapper.getList(type);
        return  result;
    }
}
